<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">我的建议</block>
		</cu-custom>
		<view v-if="isInit" :style="'height:'+screenHeight+'px;'">
			
			<!-- 列表部分 begin -->
			<mescroll-uni ref="mescrollRef" :fixed="false" top="0" :bottom="0"  @init="mescrollInit" @down="downCallback" :up="upOption" @up="upCallback">
			
				<mescroll-empty v-if="list.length==0"></mescroll-empty>
				
				<view class="cu-card dynamic">
					<view class="cu-item shadow padding"  v-for="(item,index) in list" :index="index" :item="item" :key="index">
						<view class="flex justify-between">
							<view>
								<text :class="'text-bold '+(item.id==dataId?'text-red':'')">建议:{{item.suggestion}}</text> 
								<text class="text-grey margin-left-sm text-sm" style="display: inline-block;">
									[{{getDateDiff(item.createTime)}}]
								</text>
							</view>
							<button class="cu-btn radius sm bg-red" @tap="remove(item)">
								删除
							</button>
						</view>
						<view v-if="item.isRead" class="flex justify-start">
							<view class="margin-top-sm">平台回复:</view>
							<rich-text class="margin-top-sm" :nodes="item.answer"></rich-text>
						</view>
						<view v-else class="text-gray margin-top-sm text-sm flex justify-between">
							平台会第一时间回复您的建议。
						</view>
					</view>
				</view>
			
			</mescroll-uni>
			<!-- 列表部分 end -->
			
			<!-- 操作条部分 begin -->
			<view class="cu-bar flex justify-center bg-white text-center foot solid-top">
				<button class="cu-btn round bg-blue" style="width: 80%;" @tap="goSuggestion">我有建议</button>
			</view>
			<!-- 操作条部分 end -->
			
		</view>
		<view class="cu-load load-modal" v-if="!isInit">
			<view class="gray-text">加载中...</view>
		</view>
		
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"; // 非uni_modules版本
	import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"; // 非uni_modules版本
	
	import * as confirm from '@/common/confirm.js'
	
	import {formatDateTime} from "@/common/util.js"
	
	export default {
		mixins: [MescrollMixin],
		components:{
		        MescrollBody
		},
		data() {
			return {
				//初始化函数
				isInit:false,
				dataId:0,
				upOption: {
					page: {
						size: 6 // 每页数据的数量,默认10
					},
					empty: {
						tip: '暂无我的建议',
					},
					textNoMore:'-- 没有更多建议 --',
					toTop:{
						offset:200,
						bottom: '210rpx',
					}
				},
				screenHeight:this.s_h_notb,
				list:[]
			}
		},
		onLoad(option){
			this.dataId=option.id
			this.isInit=true
		},
		onShow() {
			if(this.isInit){
				this.$nextTick(()=>{
					this.mescroll.resetUpScroll();
				});
			}
		},
		methods: {
			getDateDiff(date){
				return formatDateTime(new Date(date),'yyyy-MM-dd')
			},
			goSuggestion(){
				uni.navigateTo({
					url:"/pages/my/suggestion/suggestion"
				})
			},
			remove(item){
				let _this=this
				confirm.choose('删除该建议?',(item)=>{
					uni.showLoading({
						title:'提交中'
					})
					this.$api.post('/user/remove_suggestion',{id:item.id}).then(res => {
						uni.hideLoading()
						uni.showToast({
							title:'删除成功',
							icon:'none'
						})
						this.$nextTick(()=>{
							this.mescroll.resetUpScroll();
						});
					}).catch(()=>{
						uni.hideLoading()
					})
				},item)
			},
			upCallback(page){
				
				let _this=this
				let pageNum = page.num; // 页码, 默认从1开始
				
				let params={
					"p":pageNum,
				}
				
				this.$api.post('/user/suggestion_page',params).then(res => {
					let data=res.data;
					
					let curPageData = data.records;
					let curPageLen=curPageData.length
					let totalSize=data.total;
					
					if(page.num == 1) _this.list = []; //如果是第一页需手动置空列表
					_this.list = _this.list.concat(curPageData); //追加新数据
											
					_this.mescroll.endBySize(curPageLen, totalSize);
					
					this.$nextTick(()=>{
						if(!this.isInit){
							this.isInit=true
						}
					})
				})
			},
		}
	}
</script>

<style>

</style>
